<?php

include_once "function_xml_basic.php";

$xpath_root = "/MINI/NETWORK/SNMPS";
$xpath_node = "/SNMP";

function GetAttrListForSnmp()
{
	$returns = array("Version", "Enabled", "TrapAddr", "Local", "Contract", "Community","User", "SecLevel", "AuthProtocol", "CryProtocol", "AuthPass", "CryPass");
	return $returns;
}

function GetXmlSnmp()
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node;
	$key_array = GetAttrListForSnmp();
	
	$list = GetAttributList($query_string, $key_array);
	#for ($i = 0; $i < count($list); $i ++)
	#{
	#	foreach ( $key_array as $key)
	#		print $i. $key. " = ". $list[$i][$key]. "\n";
	#}
	return $list[0];
}

function SetXmlSnmp($in_array )
{
	global $xpath_root;
	global $xpath_node;

	$number = 1;
	$query_string = $xpath_root. $xpath_node. "[". $number. "]";
	EditAllAttrOfNode($query_string, $in_array);
	ApplyXmlSnmp();
}

function ApplyXmlSnmp()
{
	$command = "";
	$ret_list = GetXmlSnmp();
	
	$string1 = "com2sec notConfigUser default ". $ret_list['Community'];
	$string2 = "group notConfigGroup v1 notConfigUser";
	$string3 = "group notConfigGroup v2c notConfigUser";
	$string4 = "view all included .1 80";
	$string5 = "access notConfigGroup \\\"\\\" any noauth exact all none none";

	$string6 = "authtrapenable 1";
	$string7 = "trapcommunity Mini";
	$string8 = "defaultMonitors yes";
	$string9 = "linkUpDownNotifications yes ";

	$str_trap1 = "trapsink ";
	$str_trap2 = "trap2sink ";
	$str_trap3 = "informsink ";
	
	$command_kill = "/bin/kill -9 `/bin/pidof  snmpd` 1>/dev/null 2>&1;";
	$command_echo = "/bin/echo ". $string1. " > /etc/snmp/snmpd.conf;";
	$command_echo .= "/bin/echo ". $string2. " >> /etc/snmp/snmpd.conf;";
	$command_echo .= "/bin/echo ". $string3. " >> /etc/snmp/snmpd.conf;";
	$command_echo .= "/bin/echo ". $string4. " >> /etc/snmp/snmpd.conf;";
	$command_echo .= "/bin/echo ". $string5. " >> /etc/snmp/snmpd.conf;";

	$command_echo .= "/bin/echo ". $string6. " >> /etc/snmp/snmpd.conf;";
	$command_echo .= "/bin/echo ". $string7. " >> /etc/snmp/snmpd.conf;";
	$command_echo .= "/bin/echo ". $string8. " >> /etc/snmp/snmpd.conf;";
	$command_echo .= "/bin/echo ". $string9. " >> /etc/snmp/snmpd.conf;";

	$command_echo .= "/bin/echo ". $str_trap1. $ret_list['TrapAddr']. " >> /etc/snmp/snmpd.conf;";
	$command_echo .= "/bin/echo ". $str_trap2. $ret_list['TrapAddr']. " >> /etc/snmp/snmpd.conf;";
	$command_echo .= "/bin/echo ". $str_trap3. $ret_list['TrapAddr']. " >> /etc/snmp/snmpd.conf;";
	
	$command_start= "/usr/local/net-snmp/sbin/snmpd -c /etc/snmp/snmpd.conf; ";
	$command_iptables_f = "/usr/local/bin/iptables -F SNMP;";
	$command_iptables_a = "/usr/local/bin/iptables -A SNMP -p udp --dport 161 -j ACCEPT;";
	
	if ($ret_list['Enabled'] == 1)
		$command = $command_kill. $command_echo. $command_start. $command_iptables_f. $command_iptables_a;
	else
		$command = $command_kill. $command_iptables_f;
	//print $command;
	$ret = shell_exec($command);
	return $ret;
}


?>
